var requestMode = require('../../../js/requestsMode.js');
var constants = require('../../../js/constants.js');
var session = require('../../../js/session.js');
var Tools = require('../../../js/Tools.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
      address : "",
      rightImg:"../../../images/rightArrow_black.png",
      name:'',
      userType:"",
      phone:'',
      avatarUrl:'',
      uploadFiles : ''
  },
    chooseAvatar : function () {
        let This = this;
        wx.chooseImage({
            count:1,
            success: function(res) {
                var imageArray = res.tempFilePaths;
                This.setData({
                    avatarUrl: imageArray[0],
                    uploadFiles:imageArray[0]
                });
            }
        })
    },
    onShow: function (options) {
        let This = this;
        ////获取用户详细信息
        Tools.showLoaddingDefault();
        requestMode.sendRequest('member/detail','POST',{},{sKey:session.get('sKey')}).then(function(res){
            if(res.data.code == 200){
                Tools.hideLoaddingToast();
                let resData = res.data.data;
                ////需要设置收件人和手机号
                This.setData({
                    name:resData.member_nickname,
                    address:resData.address_full,
                    userType:resData.member_type,
                    phone:resData.mobile,
                    member_restaurant:resData.consignee
                });
            }else{
                Tools.showErrorToast(res.data.message);
            }
        },function(){
            Tools.showWrongNetWorkToast();
        })
    },
    onLoad: function () {
        let This = this;
        ////获取用户详细信息
        Tools.showLoaddingDefault();
        requestMode.sendRequest('member/detail','POST',{},{sKey:session.get('sKey')}).then(function(res){
            if(res.data.code == 200){
                Tools.hideLoaddingToast();
                let resData = res.data.data;
                ////需要设置收件人和手机号
                This.setData({
                    avatarUrl:resData.member_face
                });
            }
        })
    },
    submit:function(){
        let This = this;
        let uploadFiles = This.data.uploadFiles;
        let formData = {
            member_nickname:This.data.name,
            address_full:This.data.address,
            member_type:This.data.userType,
            mobile:This.data.phone,
            member_restaurant:This.data.member_restaurant
        };
        if(uploadFiles){
            wx.uploadFile({
                url: constants.requestUrl+'member/modify'+constants.lastText,
                filePath: uploadFiles,
                name: 'member_face',
                formData:formData,
                header: { "Content-Type": "multipart/form-data" , 'sKey':session.get('sKey') },
                success: function(res){
                    let data = JSON.parse(res.data);
                    let code = data.code;
                    if(code == 200){
                        session.set('member_face',data.data.member_face);
                        Tools.showSuccessInfoToast("更新成功")
                    }else{
                        Tools.showErrorToast("更新失败");
                    }
                },
                fail: function (e) {
                    Tools.showWrongNetWorkToast();
                }
            });
        }else{
            requestMode.sendRequest("member/modify",'POST',formData,{'sKey':session.get('sKey')}).then(function(res){
                let code = res.data.code;
                if(code == 200){
                    Tools.showSuccessInfoToast("更新成功")
                }else{
                    Tools.showErrorToast("更新失败");
                }
            },function(){
                Tools.showWrongNetWorkToast();
            })
        }

    },
    nameChange:function(e){
        let name = e.detail.value;
        this.setData({
            name : name
        })
    },
    restaurantChange:function(e){
        let name = e.detail.value;
        this.setData({
            member_restaurant : name
        })
    },
    choseAddress:function(){
        wx.navigateTo({
            url: '../addressManager/addressManager'
        })
    },
  onReady: function () {},
  onHide: function () {

  },
  onUnload: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});